package com.sun.local.top;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

import static com.sun.common.constants.Constant.SSQ_FILE_PATH;

/**
 * 自定义统计排行
 */
public class TopCustomize {

    public static void main(String[] args) throws Exception {
        getTopYearData();
    }

    private static void getTopYearData() throws IOException {
        // 读取文件数据
        List<String> lines = Files.readAllLines(Paths.get(SSQ_FILE_PATH));

        // 按年进行筛选
        Map<String, List<String>> yearMap = filterYear(lines);

        for (Map.Entry<String, List<String>> yearEntry : yearMap.entrySet()) {
            String year = yearEntry.getKey();
            List<String> yearList = yearEntry.getValue();
            // 获取连号Top数据
            if (StringUtils.equals("2024", year)) {
                System.out.println("#########################" + year + "年 连号数据###########################");
                List<String> allList = new ArrayList<>();
                for (String data: yearList) {
                    // 2024-10-08|24115|03,10,11,19,27,28+07|AC=4 |SerialNumber=2|SUM=98 |OddEvenRatio=4:2|Distribution=3:1:2|
                    String[] line = data.split("\\|");
                    String num = line[1];
                    String ssq = line[2];
                    String blue = ssq.split("\\+")[0];
                    List<String> list = findConsecutiveNumbers(blue);
                    if(CollectionUtils.isNotEmpty(list)) {
                        allList.addAll(list);
                        System.out.println("第" + num + "期 连号：" + list);
                    }
                }
                System.out.println("#########################" + year + "年 连号次数###########################");
                Map<String, Integer> frequencyMap = countFrequency(allList);
                List<Map.Entry<String, Integer>> sortedEntries = sortByValueDescending(frequencyMap);
                for (Map.Entry<String, Integer> entry : sortedEntries) {
                    System.out.println(entry.getKey() + " 出现次数: " + entry.getValue());
                }
            }
        }
    }

    public static Map<String, Integer> countFrequency(List<String> list) {
        Map<String, Integer> frequencyMap = new HashMap<>();
        for (String item : list) {
            frequencyMap.put(item, frequencyMap.getOrDefault(item, 0) + 1);
        }
        return frequencyMap;
    }

    public static List<Map.Entry<String, Integer>> sortByValueDescending(Map<String, Integer> map) {
        List<Map.Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
        entries.sort((a, b) -> b.getValue().compareTo(a.getValue()));
        return entries;
    }

    public static List<String> findConsecutiveNumbers(String input) {
        String[] parts = input.split(",");
        List<String> consecutiveNumbers = new ArrayList<>();
        for (int i = 0; i < parts.length - 1; i++) {
            int current = Integer.parseInt(parts[i]);
            int next = Integer.parseInt(parts[i + 1]);
            if (next - current == 1) {
                consecutiveNumbers.add(parts[i] + "," + parts[i + 1]);
            }
        }
        return consecutiveNumbers;
    }

    /**
     * 按年过滤数据
     */
    private static Map<String, List<String>> filterYear(List<String> lines) {
        Map<String, List<String>> yearMap = new LinkedHashMap<>();
        for (String ssq : lines) {
            // 年份-2023-08-40
            String year = ssq.substring(0, 4);
            // 双色球列表
            List<String> ssqList = yearMap.get(year);
            if(CollectionUtils.isNotEmpty(ssqList)) {
                ssqList.add(ssq);
            } else {
                ssqList = new ArrayList<>();
                ssqList.add(ssq);
            }
            yearMap.put(year, ssqList);
        }
        return yearMap;
    }

}
